package com.huobao.yunying.activitys;

import org.xutils.view.annotation.ViewInject;

import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.zhangying.R;

import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class Act_WebView extends HuobaoBaseActivity implements Runnable{
	@ViewInject(R.id.webview)
	private WebView m_WebView;

	// 进度
	@ViewInject(R.id.web_pb)
	private ProgressBar progressBar;

	private String title;

	@Override
	protected int getContentView() {
		return R.layout.layout_webview;
	}

	@Override
	protected String getTitleText() {
		return getIntent().getStringExtra("title");
	}

	@Override
	protected void init() {
		title = getIntent().getStringExtra("title");
		mAbTitleBar.setVisibility(title == null ? View.GONE : View.VISIBLE);
		progressBar.setVisibility(View.VISIBLE);
		progressBar.setProgress(0);
		m_WebView.setScrollContainer(false);
		m_WebView.setScrollbarFadingEnabled(false);
		m_WebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
		m_WebView.setHorizontalScrollBarEnabled(false);// 设置水平滚动条
		m_WebView.setVerticalScrollBarEnabled(false);// 设置竖直滚动条
		m_WebView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
				Message message = Message.obtain();
				message.arg1 = newProgress;
				message.what = 100;
				handler.sendMessage(message);
			}

		});
		m_WebView.setWebViewClient(new WebViewClient(){

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return super.shouldOverrideUrlLoading(view, url);
			}
			
		});
		WebSettings settings = m_WebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
		String url = getIntent().getStringExtra("url");
		m_WebView.loadUrl(url);
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 100) {
				int progress = msg.arg1;
				if (progress == 100)
					handler.postDelayed(Act_WebView.this, 800);
				progressBar.setProgress(progress);
			}
		};
	};
	@Override
	public void run() {
		progressBar.setVisibility(View.GONE);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			if(m_WebView.canGoBack()){
				m_WebView.goBack();
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
